package com.edu.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.backend.dao.AdminDao;
import com.edu.backend.dao.UserDao;
import com.edu.backend.entity.Admin;
import com.edu.backend.entity.PagerInfo;
import com.edu.backend.entity.User;
import com.edu.backend.service.AdminService;
import com.edu.backend.service.UserService;
import com.edu.backend.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yanjunfeng
 * @Description: TODO
 * @Date: Created in 下午1:57 2023/6/20
 * @Modified By:
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Override
    public PagerInfo<User> getStudentList(PagerInfo<User> pageInfo, User user) {
        // 条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        System.out.println(user.toString());
        if(user.getUsername()!=null&&user.getUsername()!="") {
            // 模糊查询Like
            queryWrapper.like("username", user.getUsername());
        }
        queryWrapper.eq("role","student");
        // 分页插件
        Page<User> page = new Page(pageInfo.getCurrent(), pageInfo.getSize());
        page = this.page(page, queryWrapper);
        return new PagerInfo<>(page.getTotal(), page.getRecords(),page.getPages(),page.getCurrent(),page.getSize());
    }

    @Override
    public PagerInfo<User> getTeacherList(PagerInfo<User> pageInfo, User user) {
        // 条件构造器
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        System.out.println(user.toString());
        if(user.getUsername()!=null&&user.getUsername()!="") {
            // 模糊查询Like
            queryWrapper.like("username", user.getUsername());
        }
        queryWrapper.eq("role","teacher");
        // 分页插件
        Page<User> page = new Page(pageInfo.getCurrent(), pageInfo.getSize());
        page = this.page(page, queryWrapper);
        return new PagerInfo<>(page.getTotal(), page.getRecords(),page.getPages(),page.getCurrent(),page.getSize());
    }

    @Override
    public Boolean deleteStudent(String id) {

        Boolean result = this.removeById(id);
        return result;
    }

    @Override
    public Boolean deleteTeacher(String id) {
        Boolean result = this.removeById(id);
        return result;
    }

    @Override
    public Boolean deleteBatch(List<String> ids){
        Boolean result = this.removeByIds(ids);
        return result;
    }

    @Override
    public Boolean addStudent(User user){
        user.setPassword(MD5.encrypt(user.getPassword()));
        user.setRole("student");
        user.setStatus(0);
        Boolean result = this.save(user);
        return result;
    }

    @Override
    public Boolean addTeacher(User user) {
        user.setPassword(MD5.encrypt(user.getPassword()));
        user.setRole("teacher");
        user.setStatus(0);
        Boolean result = this.save(user);
        return result;
    }

    @Override
    public Boolean updateStudent(User user){
        user.setPassword(MD5.encrypt(user.getPassword()));
        Boolean result = this.updateById(user);
        return result;
    }
    @Override
    public User getStudent(Integer id){
        User user = this.getById(id);
        return user;
    }
}
